<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Rust的多继承和mixin机制 - __JM_Joy_Blog__</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="__JM_Joy__" /><meta name="description" content="先上一段Rust代码： 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 traitA{fn say(self);}traitB{fn say(self);}struct S;implAforS{fn say(self){println!(&amp;#34;S say: A&amp;#34;);}}implBforS{fn say(self){println!(&amp;#34;S say: B&amp;#34;);}}fn say_a&amp;lt;T: A&amp;gt;(t: T){t.say();}fn say_b&amp;lt;T: B&amp;gt;(t: T){t.say();}fn main(){say_a(S);say_b(S);} 给java和p" /><meta name="keywords" content="Hugo, theme, even" />






<meta name="generator" content="Hugo 0.53 with even 4.0.0" />


<link rel="canonical" href="https://jmjoy.github.io/post/rust%E7%9A%84%E5%A4%9A%E7%BB%A7%E6%89%BF%E5%92%8Cmixin%E6%9C%BA%E5%88%B6/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">


<link href="/dist/even.8e23cf7d.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="Rust的多继承和mixin机制" />
<meta property="og:description" content="先上一段Rust代码： 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 traitA{fn say(self);}traitB{fn say(self);}struct S;implAforS{fn say(self){println!(&#34;S say: A&#34;);}}implBforS{fn say(self){println!(&#34;S say: B&#34;);}}fn say_a&lt;T: A&gt;(t: T){t.say();}fn say_b&lt;T: B&gt;(t: T){t.say();}fn main(){say_a(S);say_b(S);} 给java和p" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://jmjoy.github.io/post/rust%E7%9A%84%E5%A4%9A%E7%BB%A7%E6%89%BF%E5%92%8Cmixin%E6%9C%BA%E5%88%B6/" /><meta property="article:published_time" content="2016-05-23T19:22:03&#43;08:00"/>
<meta property="article:modified_time" content="2016-05-23T19:22:03&#43;08:00"/>

<meta itemprop="name" content="Rust的多继承和mixin机制">
<meta itemprop="description" content="先上一段Rust代码： 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 traitA{fn say(self);}traitB{fn say(self);}struct S;implAforS{fn say(self){println!(&#34;S say: A&#34;);}}implBforS{fn say(self){println!(&#34;S say: B&#34;);}}fn say_a&lt;T: A&gt;(t: T){t.say();}fn say_b&lt;T: B&gt;(t: T){t.say();}fn main(){say_a(S);say_b(S);} 给java和p">


<meta itemprop="datePublished" content="2016-05-23T19:22:03&#43;08:00" />
<meta itemprop="dateModified" content="2016-05-23T19:22:03&#43;08:00" />
<meta itemprop="wordCount" content="328">



<meta itemprop="keywords" content="" />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Rust的多继承和mixin机制"/>
<meta name="twitter:description" content="先上一段Rust代码： 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 traitA{fn say(self);}traitB{fn say(self);}struct S;implAforS{fn say(self){println!(&#34;S say: A&#34;);}}implBforS{fn say(self){println!(&#34;S say: B&#34;);}}fn say_a&lt;T: A&gt;(t: T){t.say();}fn say_b&lt;T: B&gt;(t: T){t.say();}fn main(){say_a(S);say_b(S);} 给java和p"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">__JM_Joy_Blog__</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">Home</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">Archives</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">Tags</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">Categories</li>
      </a><a href="/about/">
        <li class="mobile-menu-item">About</li>
      </a>
  </ul>
</nav>
  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">__JM_Joy_Blog__</a>
</div>

<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">Home</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">Archives</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">Tags</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">Categories</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/about/">About</a>
      </li>
  </ul>
</nav>
    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">Rust的多继承和mixin机制</h1>

      <div class="post-meta">
        <span class="post-time"> 2016-05-23 </span>
        <div class="post-category">
            <a href="/categories/%E5%85%B6%E5%AE%83/"> 其它 </a>
            </div>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    
  </div>
</div>
    <div class="post-content">
      <p>先上一段Rust代码：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code class="language-rust" data-lang="rust"><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-rust" data-lang="rust"><span class="k">trait</span><span class="w"> </span><span class="n">A</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="w">    </span><span class="k">fn</span> <span class="nf">say</span><span class="p">(</span><span class="bp">self</span><span class="p">);</span><span class="w">
</span><span class="w"></span><span class="p">}</span><span class="w">
</span><span class="w">
</span><span class="w"></span><span class="k">trait</span><span class="w"> </span><span class="n">B</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="w">    </span><span class="k">fn</span> <span class="nf">say</span><span class="p">(</span><span class="bp">self</span><span class="p">);</span><span class="w">
</span><span class="w"></span><span class="p">}</span><span class="w">
</span><span class="w">
</span><span class="w"></span><span class="k">struct</span> <span class="nc">S</span><span class="p">;</span><span class="w">
</span><span class="w">
</span><span class="w"></span><span class="k">impl</span><span class="w"> </span><span class="n">A</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">S</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="w">    </span><span class="k">fn</span> <span class="nf">say</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="w">        </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&#34;S say: A&#34;</span><span class="p">);</span><span class="w"> 
</span><span class="w">    </span><span class="p">}</span><span class="w">
</span><span class="w"></span><span class="p">}</span><span class="w">
</span><span class="w">
</span><span class="w"></span><span class="k">impl</span><span class="w"> </span><span class="n">B</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">S</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="w">    </span><span class="k">fn</span> <span class="nf">say</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="w">        </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&#34;S say: B&#34;</span><span class="p">);</span><span class="w"> 
</span><span class="w">    </span><span class="p">}</span><span class="w">
</span><span class="w"></span><span class="p">}</span><span class="w">
</span><span class="w">
</span><span class="w"></span><span class="k">fn</span> <span class="nf">say_a</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">A</span><span class="o">&gt;</span><span class="p">(</span><span class="n">t</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="w">    </span><span class="n">t</span><span class="p">.</span><span class="n">say</span><span class="p">();</span><span class="w">
</span><span class="w"></span><span class="p">}</span><span class="w">
</span><span class="w">
</span><span class="w"></span><span class="k">fn</span> <span class="nf">say_b</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">B</span><span class="o">&gt;</span><span class="p">(</span><span class="n">t</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="w">    </span><span class="n">t</span><span class="p">.</span><span class="n">say</span><span class="p">();</span><span class="w">
</span><span class="w"></span><span class="p">}</span><span class="w">
</span><span class="w">
</span><span class="w"></span><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="w">    </span><span class="n">say_a</span><span class="p">(</span><span class="n">S</span><span class="p">);</span><span class="w">
</span><span class="w">    </span><span class="n">say_b</span><span class="p">(</span><span class="n">S</span><span class="p">);</span><span class="w">
</span><span class="w"></span><span class="p">}</span><span class="w">
</span><span class="w"></span></code></pre></td></tr></table>
</div>
</div>
<p>给java和php的童鞋的解释就是：两个接口A和B，一个类S，S分别针对A和B实现了say()这个方法。</p>

<p>因为这在java或php(抄袭java的面向对象)是不可能的，万恶的根源在于他们是将类的所有方法都写在类这个域里面，然后得出“多继承”可能会导致函数冲突(两个父类拥有相同原型的函数)的荒谬理论。</p>

<p>在Rust中，得益于成员函数写在了struct之外(而且可以是多个impl的域)，函数冲突的问题重来就没有过。</p>

<p>另外Rust的trait可以有默认实现，和有函数实体的父类异曲同工。</p>

<p>====</p>

<p>2016.6.10</p>

<p>Rust没有继承，struct之间并不能复用代码。</p>

    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">__JM_Joy__</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">2016-05-23</span>
  </p>
  
  
</div><footer class="post-footer">
      
      <nav class="post-nav">
        <a class="prev" href="/post/%E5%A4%9A%E7%BB%A7%E6%89%BF%E5%AE%8C%E7%BE%8E%E9%97%AA%E9%81%BF%E6%88%90%E5%91%98%E5%90%8D%E5%86%B2%E7%AA%81%E7%9A%84%E9%97%AE%E9%A2%98/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">多继承完美闪避成员名冲突的问题</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/emacsspacemacs-on-windows/">
            <span class="next-text nav-default">Emacs(Spacemacs) on Windows</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="https://github.com/jmjoy" class="iconfont icon-github" title="github"></a>
  <a href="https://jmjoy.github.io/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    &copy; 
    2017 - 
    2019
    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">__JM_Joy__</span>
  </span>
</div>
    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>
<script type="text/javascript" src="/dist/even.26188efa.min.js"></script>








</body>
</html>
